package com.bank.config;

import com.bank.entity.Member;
import com.bank.um.service.Impl.MemberServiceImpl;
import com.bank.utils.CookieUtils;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: Lixy
 * @email: 1908598362@qq.com
 * @date: 2022/1/27 星期四 23:17
 */
@Component
public class MemberArgumentResolver implements HandlerMethodArgumentResolver {

    @Autowired
    private MemberServiceImpl memberService;

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        Class<?> clazz = parameter.getParameterType();
        return clazz == Member.class;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest nativeRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        HttpServletResponse nativeResponse = webRequest.getNativeResponse(HttpServletResponse.class);
        String memeberUUID = CookieUtils.getCookieValue(nativeRequest, "MemeberUUID");
        if (StringUtils.isNullOrEmpty(memeberUUID)) {
            return null;
        }
        return memberService.SelectByCoolredis(memeberUUID, nativeResponse, nativeRequest);
    }
}
